<?php
namespace app\index\controller;

use think\api\Client;
use think\cache\driver\Redis;
use think\Controller;

class Index extends Controller
{
    public function index(){
        if(request()->url() == "/"){
            $url = "/index/index/index";
        }else{
            $url = "/index/index/index";
        }
//        dump(Common::indexGoodsCate(0));die;
        $this->assign([
            'url' => $url,//方法
            'indexMenu' => Common::indexMenu(),//首页按钮
            'setting' => Common::indexSetting(), //设置
            'indexNav' => Common::indexNav(),//首页按钮
            'indexGoodsCate' => Common::indexGoodsCate(0),//首页按钮
            'typeGoodsOne' => Common::indexGoods(1,5),//热门精品
            'typeGoodsTwo' => Common::indexGoods(2,4),//精选分类
            'typeGoodsThree' => Common::indexGoods(3,1),//精选好物
        ]);
        return view("index");
    }
    public function about(){
        $this->assign([
            'url' => request()->url(),//方法
            'indexMenu' => Common::indexMenu(),//首页按钮
            'setting' => Common::indexSetting(), //设置
            'aboutCopyDescr' => Common::aboutCopyDescr(), //描述
        ]);
        return view("about");
    }
    public function connect(){
//        dump(Common::indexSetting());
        $this->assign([
            'url' => request()->url(),//方法
            'indexMenu' => Common::indexMenu(),//首页按钮
            'setting' => Common::indexSetting(), //设置
        ]);
        return view("connect");
    }
    //联系我们
    public function postConnect()
    {
        if(request()->post()){
            $param = request()->param();
            $data = $param;
            $data['ctime'] = date("Y-m-d H:i:s",time());
            $res = db("connect")->insert($data);
            if(!$res) return ['code' => -1,'msg' => '系统错误  请稍后再试'];
            return ['code' => 200,'msg' => '发送成功 我们人工将在24小时之内与您联系'];
        }
    }
    //详情页
    public function detail()
    {
        $cate_id = (int)input("cate_id") ?? 0;
        //查询出来 大分类下的子分类 以及子分类所属的产品
        if(empty($cate_id)){
            $cate_list = db("goods_cate")->where('pid','>',$cate_id)->select();
//            dump($cate_list);
        }else{
            $cate_list = db("goods_cate")->where('pid',$cate_id)->select();
//            dump(111);
        }
//        die;
        foreach($cate_list as $key=>$value){
            $cate_list[$key]['goods_list'] = db("goods")->where("cate_id",$value['cate_id'])->select();
        }
//        die;
        $cate = db("goods_cate")->find($cate_id);
        $this->assign([
            'url' => "/index/index/index",//方法
            'indexMenu' => Common::indexMenu(),//首页按钮
            'setting' => Common::indexSetting(), //设置
            'findCate' => $cate,
            'cate_list' => $cate_list
//            'typeGoodsThree' => Common::indexGoods($type,1),//精选好物
        ]);
        return view("detail");
    }
    public function setting()
    {
        $setting = db("setting")->order("id desc")->find();//查询设置
//        dump($setting);die;
        $index_menu_arr = explode("#",$setting['index_menu']);//首页按钮
        $footer_about = explode("#",$setting['footer_about']);//关于七星
        $footer_about_we = explode("#",$setting['footer_about_we']);//关于我们
        $data = [
            'setting' => $setting,
            'index_menu_arr' => $index_menu_arr,
            'footer_about' => $footer_about,
            'footer_about_we' => $footer_about_we,
        ];
        return $data;
    }




































    public function nav()
    {
        $nav = db("nav")->select();
        return $nav;
    }
    public function pv()
    {
        $num = input("val");
        $redis = new Redis();
        $key = date("Y-m-d",time());
        $total_key = 'total_key';
        $val = $redis->get($key);
        $total_val = $redis->get($total_key);
        if(!$total_val){
            $redis->set($total_key,1);
        }else{
            $total_val +=1;
            $redis->set($total_key,$total_val);
        }
        if(empty($val)){
            $redis->set($key,$num);
        }else{
            $val +=1;
            $redis->set($key,$val);
        }
    }
    public function checkData()
    {
        $id_card = input("id_card");
//        dump($id_card);
        $client = new Client("65699726a3c601b9f31bf04019c8593c");
        $result = $client->idcardIndex()
            ->withCardno($id_card)
            ->request();
        return json($result);
//        dump($result);
    }
}
